package com.asohofrucol.web;

import javax.faces.event.ActionEvent;

import org.primefaces.component.commandlink.CommandLink;
import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.asohofrucol.dto.AsohofrucolSessionDTO;

public class StepByStepController {
	@Autowired
	private AsohofrucolSessionDTO aSession;
	@Autowired
	private RuatDataController ruatDataController;
	private int stepNumber;
	public static String RUAT_EMPTY_STATUS = "0";
	public static String RUAT_EMPTY_FLAG = "empty";

	public void viewPersonalData(ActionEvent actionEvent) {
		System.out.println("viewPersonalData");
		RequestContext context = RequestContext.getCurrentInstance();
		ruatDataController.saveRuatRegister(actionEvent);
		stepNumber = 1;
		context.addCallbackParam("view", "personal_data.xhtml");
	}

	public void viewContactData(ActionEvent actionEvent) {
		System.out.println("viewContactData");
		Long idregister = aSession.getSelectedRuatRegister().getId();
		Boolean exist = ruatDataController.registerAlreadyExisist(actionEvent);
		System.out.println("la clase!!");
		System.out.println(actionEvent.getSource().getClass()
				.getCanonicalName());
		if (actionEvent.getSource() instanceof CommandLink) {
			if (isStep1()) {
				this.buildCallbackParam("contact_data.xhtml");
			} else {
				//this.buildCallbackParam("personal_data.xhtml");
			}
		} else {
			if(!isStep1())aSession.getSelectedRuatRegister().setStatus("1");
			if (exist && (idregister == null)) {
				ruatDataController.loadRuat(actionEvent);
				aSession.getSelectedRuatRegister().setEditing(true);
				this.buildCallbackParam("personal_data.xhtml");
			} else {
				ruatDataController.saveRuatRegister(actionEvent);
				stepNumber = 2;
				this.buildCallbackParam("contact_data.xhtml");
			}
		}
	}

	public void viewEconomicData(ActionEvent actionEvent) {
		System.out.println("viewEconomicData");
		if (actionEvent.getSource() instanceof CommandLink) {
			if (isStep2()) {
				this.buildCallbackParam("economic_data.xhtml");
			} else {
				//this.buildCallbackParam("contact_data.xhtml");
			}
		} else {
			if(!isStep2())aSession.getSelectedRuatRegister().setStatus("2");
			ruatDataController.saveRuatRegister(actionEvent);
			stepNumber = 3;
			this.buildCallbackParam("economic_data.xhtml");
		}
	}

	public void viewInnovationData(ActionEvent actionEvent) {
		System.out.println("viewInnovationData");
		if (actionEvent.getSource() instanceof CommandLink) {
			if (isStep3()) {
				this.buildCallbackParam("innovation_data.xhtml");
				ruatDataController.loadProcessTransferInnovationData(actionEvent);
			} else {
				//this.buildCallbackParam("economic_data.xhtml");
			}
		} else {
			// por defecto se salta un paso en el cual nunca se llena informacion 
//			if(!isStep3())aSession.getSelectedRuatRegister().setStatus("3");
//			ruatDataController.saveRuatRegister(actionEvent);
//			ruatDataController.loadProcessTransferInnovationData(actionEvent);
//			stepNumber = 4;
//			this.buildCallbackParam("innovation_data.xhtml");
			
			if(!isStep3())aSession.getSelectedRuatRegister().setStatus("4");
			ruatDataController.saveRuatRegister(actionEvent);
			ruatDataController.loadAssociativitytBusinessData(actionEvent);
			stepNumber = 5;
			this.buildCallbackParam("associativity_data.xhtml");			
		}
	}

	public void viewAssociativityData(ActionEvent actionEvent) {
		System.out.println("viewAssociativityData");
		if (actionEvent.getSource() instanceof CommandLink) {
			if (isStep4()) {
				this.buildCallbackParam("associativity_data.xhtml");
				ruatDataController.loadAssociativitytBusinessData(actionEvent);
			} else {
				//this.buildCallbackParam("innovation_data.xhtml");
			}
		} else {
			if(!isStep4())aSession.getSelectedRuatRegister().setStatus("4");
			ruatDataController.saveRuatRegister(actionEvent);
			ruatDataController.saveProcessTransferInnovation(actionEvent);
			ruatDataController.loadAssociativitytBusinessData(actionEvent);
			stepNumber = 5;
			this.buildCallbackParam("associativity_data.xhtml");
		}
	}

	public void viewAssociativityDataB(ActionEvent actionEvent) {
		System.out.println("viewAssociativityData");
		if (actionEvent.getSource() instanceof CommandLink) {
			if (isStep5()) {
				this.buildCallbackParam("associativity_dataB.xhtml");
			} else {
				//this.buildCallbackParam("associativity_data.xhtml");
			}
		} else {
			if(!isStep5())aSession.getSelectedRuatRegister().setStatus("5");
			ruatDataController.saveRuatRegister(actionEvent);
			ruatDataController.saveAssocitivities(actionEvent);
			stepNumber = 6;
			this.buildCallbackParam("associativity_dataB.xhtml");
		}
	}

	public void viewFarmData(ActionEvent actionEvent) {
		System.out.println("viewFarmData");
		if (actionEvent.getSource() instanceof CommandLink) {
			if (isStep6()) {
				this.buildCallbackParam("farm_data.xhtml");
			} else {
				//this.buildCallbackParam("associativity_dataB.xhtml");
			}
		} else {
			if(!isStep6())aSession.getSelectedRuatRegister().setStatus("6");
			ruatDataController.saveRuatRegister(actionEvent);
			stepNumber = 7;
			this.buildCallbackParam("farm_data.xhtml");
		}
	}

	public void viewFarmDataB(ActionEvent actionEvent) {
		System.out.println("viewFarmData");
		if (actionEvent.getSource() instanceof CommandLink) {
			if (isStep7()) {
				this.buildCallbackParam("farm_dataB.xhtml");
			} else {
				//this.buildCallbackParam("farm_data.xhtml");
			}
		} else {
			if(!isStep7())aSession.getSelectedRuatRegister().setStatus("7");
			ruatDataController.saveRuatRegister(actionEvent);
			stepNumber = 8;
			this.buildCallbackParam("farm_dataB.xhtml");
		}
	}

	public void viewAgrarianBusinessData(ActionEvent actionEvent) {
		System.out.println("viewAgrarianBusinessData");
		if (actionEvent.getSource() instanceof CommandLink) {
			if (isStep8()) {
				this.buildCallbackParam("agrarian_business_data.xhtml");
				ruatDataController.loadAgrarianBusinessData(actionEvent);
			} else {
				//this.buildCallbackParam("farm_dataB.xhtml");
			}
		} else {
			if(!isStep8())aSession.getSelectedRuatRegister().setStatus("8");
			ruatDataController.saveRuatRegister(actionEvent);
			ruatDataController.loadAgrarianBusinessData(actionEvent);
			stepNumber = 9;
			this.buildCallbackParam("agrarian_business_data.xhtml");
		}
	}

	public void viewAnimalBusinessData(ActionEvent actionEvent) {
		System.out
				.println("viewAnimalBusinessData before livestockBusinessData");
		if (actionEvent.getSource() instanceof CommandLink) {
			if (isStep9()) {
				this.buildCallbackParam("animal_business_data.xhtml");
				ruatDataController.loadAnimalBusinessData(actionEvent);
			} else {
				//this.buildCallbackParam("agrarian_business_data.xhtml");
			}
		} else {
			if(!isStep9())aSession.getSelectedRuatRegister().setStatus("9");
			ruatDataController.saveRuatRegister(actionEvent);
			ruatDataController.saveAgrarianBusinessData(actionEvent);
			ruatDataController.loadAnimalBusinessData(actionEvent);
			stepNumber = 10;
			this.buildCallbackParam("animal_business_data.xhtml");
		}
	}

	public void viewForestBusinessData(ActionEvent actionEvent) {
		System.out.println("viewForestBusinessData");
		if (actionEvent.getSource() instanceof CommandLink) {
			if (isStep10()) {
				this.buildCallbackParam("forest_business_data.xhtml");
				ruatDataController.loadForestBusinessData(actionEvent);
			} else {
				//this.buildCallbackParam("animal_business_data.xhtml");
			}
		} else {
			//por defecto se salta un paso en el cual nunca se llena informacion
//			if(!isStep10())aSession.getSelectedRuatRegister().setStatus("10");
//			ruatDataController.saveRuatRegister(actionEvent);
//			ruatDataController.loadForestBusinessData(actionEvent);
//			ruatDataController.saveAnimalBusinessData(actionEvent);
//			stepNumber = 11;
//			this.buildCallbackParam("forest_business_data.xhtml");
			
			if(!isStep10())aSession.getSelectedRuatRegister().setStatus("11");
			ruatDataController.saveRuatRegister(actionEvent);
			ruatDataController.saveAnimalBusinessData(actionEvent);
			ruatDataController.loadSurvey(actionEvent);
			ruatDataController.loadForestBusinessData(actionEvent);
			ruatDataController.loadAnimalBusinessData(actionEvent);
			ruatDataController.loadAgrarianBusinessData(actionEvent);
			ruatDataController.loadPriorityProduct(actionEvent);			
			stepNumber = 12;
			this.buildCallbackParam("ownership_learning_data.xhtml");
		}
	}

	public void viewOwnershipLearningData(ActionEvent actionEvent) {
		System.out.println("viewOwnershipLearningData");
		if (actionEvent.getSource() instanceof CommandLink) {
			if (isStep11()) {
				this.buildCallbackParam("ownership_learning_data.xhtml");
				ruatDataController.loadSurvey(actionEvent);
				ruatDataController.loadForestBusinessData(actionEvent);
				ruatDataController.loadAnimalBusinessData(actionEvent);
				ruatDataController.loadAgrarianBusinessData(actionEvent);
				ruatDataController.loadPriorityProduct(actionEvent);
			} else {
				//this.buildCallbackParam("forest_business_data.xhtml");
			}
		} else {
			if(!isStep11())aSession.getSelectedRuatRegister().setStatus("11");
			ruatDataController.saveRuatRegister(actionEvent);
			ruatDataController.saveForestBusinessData(actionEvent);
			ruatDataController.loadSurvey(actionEvent);
			ruatDataController.loadForestBusinessData(actionEvent);
			ruatDataController.loadAnimalBusinessData(actionEvent);
			ruatDataController.loadAgrarianBusinessData(actionEvent);
			ruatDataController.loadPriorityProduct(actionEvent);
			stepNumber = 12;
			this.buildCallbackParam("ownership_learning_data.xhtml");
		}
	}

	public void viewConfirmationData(ActionEvent actionEvent) {
		System.out.println("viewConfirmationData");
		// ruatDataController.saveRuatRegister(actionEvent);
		if (actionEvent.getSource() instanceof CommandLink) {
			if (isStep12()) {
				this.buildCallbackParam("confirmation_data.xhtml");
				ruatDataController.loadSurvey(actionEvent);
				ruatDataController.loadForestBusinessData(actionEvent);
				ruatDataController.loadAnimalBusinessData(actionEvent);
				ruatDataController.loadAgrarianBusinessData(actionEvent);
			} else {
				//this.buildCallbackParam("ownership_learning_data.xhtml");
			}
		} else {
			if(!isStep12())aSession.getSelectedRuatRegister().setStatus("12");
			ruatDataController.saveRuatRegister(actionEvent);
			ruatDataController.saveSurvey(actionEvent);
			ruatDataController.savePriorityProduct(actionEvent);
			ruatDataController.loadForestBusinessData(actionEvent);
			ruatDataController.loadAnimalBusinessData(actionEvent);
			ruatDataController.loadAgrarianBusinessData(actionEvent);
			stepNumber = 13;
			this.buildCallbackParam("confirmation_data.xhtml");
		}
	}

	public void buildCallbackParam(String nextPage) {
		RequestContext context = RequestContext.getCurrentInstance();
		String ruatStatus = aSession.getSelectedRuatRegister().getStatus();
		if (ruatStatus == null) {
			aSession.getSelectedRuatRegister().setStatus(RUAT_EMPTY_STATUS);
			context.addCallbackParam("view", RUAT_EMPTY_FLAG);
		} else if (ruatStatus.equals(RUAT_EMPTY_STATUS)) {
			context.addCallbackParam("view", RUAT_EMPTY_FLAG);
		} else {
			context.addCallbackParam("view", nextPage);
		}
	}

	public boolean isStep1() {
		int status = aSession.getSelectedRuatRegister().getIntStatus();
		return (status >= 1);
	}

	public boolean isStep2() {
		int status = aSession.getSelectedRuatRegister().getIntStatus();
		return (status >= 2);
	}

	public boolean isStep3() {
		int status = aSession.getSelectedRuatRegister().getIntStatus();
		System.out.println("status " + status);
		return (status >= 3);
	}

	public boolean isStep4() {
		int status = aSession.getSelectedRuatRegister().getIntStatus();
		return (status >= 4);
	}

	public boolean isStep5() {
		int status = aSession.getSelectedRuatRegister().getIntStatus();
		return (status >= 5);
	}

	public boolean isStep6() {
		int status = aSession.getSelectedRuatRegister().getIntStatus();
		return (status >= 6);
	}

	public boolean isStep7() {
		int status = aSession.getSelectedRuatRegister().getIntStatus();
		return (status >= 7);
	}

	public boolean isStep8() {
		int status = aSession.getSelectedRuatRegister().getIntStatus();
		return (status >= 8);
	}

	public boolean isStep9() {
		int status = aSession.getSelectedRuatRegister().getIntStatus();
		return (status >= 9);
	}

	public boolean isStep10() {
		int status = aSession.getSelectedRuatRegister().getIntStatus();
		return (status >= 10);
	}

	public boolean isStep11() {
		int status = aSession.getSelectedRuatRegister().getIntStatus();
		return (status >= 11);
	}

	public boolean isStep12() {
		int status = aSession.getSelectedRuatRegister().getIntStatus();
		return (status >= 12);
	}

	public void cancelRuatRegister(ActionEvent actionEvent) {
		stepNumber = 0;
		ruatDataController.cancelRuatRegister(actionEvent);
	}

	public int getStepNumber() {
		return stepNumber;
	}

	public void setStepNumber(int stepNumber) {
		this.stepNumber = stepNumber;
	}

}
